#include <string>
#include <iostream>
#include "../tinyxml2-master/tinyxml2.h"
#include "../tinyxml2-master/tinyxml2.cpp"
#include "./json-develop/single_include/nlohmann/json.hpp"

using json = nlohmann::json;
using namespace tinyxml2;
using namespace std;

// string xml2json(string &src)
json xml2json(const char * src)
{
    cout << "ininininin" << endl;
    XMLDocument doc;
    // doc.Parse( src.c_str() );
    doc.Parse( src );
    
    json  root;
    XMLElement* rootElement = doc.RootElement();
    cout << rootElement->Value() << endl;
    XMLElement* child = rootElement->FirstChildElement();
    cout << child->Value() << endl;
    while(child) {
        const char* title = child->Name() ;
        const char* value = " ";
        cout << "title: " << title << endl;
        if(child->GetText()){
            const char* value = child->GetText();
            cout << "value" << value << endl;
        }
        else{
            const char* value = " ";
        }
        // const char* value = child->GetText();
        // cout << "value" << value << endl;
        child = child->NextSiblingElement();
        root[title] = value ;
    }
    // return  root.dump() ;
    cout << "ininininin" << endl;
    return root;
}

// string json2xml(string& src)
// {
//     XMLDocument xmlDoc;
//     XMLNode * pRoot = xmlDoc.NewElement("xml");
//     xmlDoc.InsertFirstChild(pRoot);
//     auto j3 = json::parse(src.c_str());
//     for (json::iterator it = j3.begin(); it != j3.end(); ++it) {
//         string key = it.key();
//         string value = it.value() ;
//         XMLElement * pElement = xmlDoc.NewElement(key.c_str()) ;
//         pElement->SetText(value.c_str()) ;
//         pRoot->InsertEndChild(pElement);
//     }
//     XMLPrinter printer;
//     pRoot->Accept( &printer );
//     return printer.CStr();
// }

int main()
{
    tinyxml2::XMLDocument doc0;
    doc0.LoadFile( "../ppt/455/ppt/slides/slide1.xml");
    XMLPrinter printer;
    doc0.Print( &printer );//将Print打印到Xmlprint类中 即保存在内存中
    // string buf;
    const char * buf;
    buf = printer.CStr();//转换成const char*类型
    cout<< buf <<endl;//buf即为创建后的XML字符串。

    // string src = "<xml>\
    //                <appid>appid-value111111</appid>\
    //                <mch_id>mch_id-value22222</mch_id>\
    //                <nonce_str>nonce_str-value3333333</nonce_str>\
    //                <transaction_id>transaction_id-value44444444</transaction_id>\
    //                <sign>sign-value5555555555</sign>\
    //             </xml>" ;
    cout << "test" <<endl;
    json json = xml2json(buf) ;
    cout << "test" <<endl;
    // string xml = json2xml(json) ;
    
    
    cout << json ;
    cout << endl ;
    // cout << xml ;
}